package day24;

import java.io.*;
import java.util.Scanner;

//通过IO流  复制整个目录（存在子文件，也有子目录）
public class Demo6 {
    public static void main(String[] args) {
        File in=new File("C:\\Users\\梅穿内裤\\Desktop\\IOTest\\aa");
        File out=new File("C:\\Users\\梅穿内裤\\Desktop\\IOTest\\bb");
        try(
            FileInputStream fis=new FileInputStream(in);
            FileOutputStream fos=new FileOutputStream(out);
            InputStreamReader isr=new InputStreamReader(fis);
            OutputStreamWriter osw=new OutputStreamWriter(fos);
            BufferedReader br=new BufferedReader(isr);
            BufferedWriter bw=new BufferedWriter(osw);) {
            String str1;
            String str2;
            File fs[]=in.listFiles();
            for (File f:fs){
                if (f.isFile()){
                    while ((str1= br.readLine())!=null){
                        bw.write(str1);
                    }
                }else if (f.isDirectory()){
                    File fs2[]=f.listFiles();
                    for (File f2:fs2){
                        while ((str2= br.readLine())!=null){
                            bw.write(str2);
                        }
                    }
                }
            }
            br.close();
            bw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

class test{
    static String oldPath;
    static String newPath;
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        System.out.print("请输入你要复制的目录:");
        oldPath=sc.next();
        System.out.print("请输入你要粘贴的目录:");
        newPath=sc.next();
        File oldFile=new File(oldPath);
        copy(oldFile);
    }
    public static void copy(File oldFile){
        File files[]=oldFile.listFiles();
        //1.如果是文件，判断文件上级目录是否存在，不存在要创建，地址也要替换成新地址
        //最后实现文件复制
        if (files==null){
            String path=oldFile.getAbsolutePath().replace(oldPath,newPath);
            File file=new File(path);
            File pfile=file.getParentFile();
            if (!pfile.exists()){
                pfile.mkdirs();
                System.out.println("创建目录成功:"+pfile.getAbsolutePath());
            }
            //复制文件
            copyFile(oldFile,file);
        }
        //2.如果是空目录,把老的地址替换成新的地址，再创建目录
        else if (files.length==0){
            String path=oldFile.getAbsolutePath().replace(oldPath,newPath);
            File file=new File(path);
            if (file.mkdirs()){
                System.out.println("创建空目录成功:"+file.getAbsolutePath());
            }
        }
        //3.如果是非空目录且存在子文件 递归处理
        else {
            for (File file:files){
                copy(file);
            }
        }
    }
    public static void copyFile(File in,File out){
        try(FileInputStream fis=new FileInputStream(in);
            FileOutputStream fos=new FileOutputStream(out);
            BufferedInputStream bis=new BufferedInputStream(fis);
            BufferedOutputStream bos=new BufferedOutputStream(fos)
        ){
          byte bs[]=new byte[1024];
          int len;
          while ((len=bis.read(bs))!=-1){
              bos.write(bs,0,len);
          }
          System.out.println("复制文件成功:"+out.getAbsolutePath());
        }catch (IOException e){
            e.printStackTrace();
        }

    }
}